package com.sch.www.ele.services.dataAuthority;


import com.sch.www.codeGenerator.base.*;

import java.util.*;


/**
 * BaseServices接口
 * Created by admin on 2017/01/07.
 */
public abstract class DataAuthorityServices<Record extends BaseEntity, Example extends BaseExample, Mapper extends BaseMapper<Record, Record, Example>, CurrentProxy>
        extends BaseServicesSimpleImp<Record, Example, Mapper, CurrentProxy> {

    @Override
    public Record selectById(String id) {
        if (id == null || id.equals("")) {
            return null;
        } else {
            Example example = getExampleNewInstance();
            example.createCriteria().andIdEqualTo(id);
            handlerExmaple(example);
            return getMapper().selectOneByExample(example);
        }
    }

    @Override
    public List<Record> selectById(Collection<String> idList) {
        if (idList == null || idList.isEmpty()) {
            return Collections.EMPTY_LIST;
        } else {
            Example example = getExampleNewInstance();
            example.createCriteria().andIdIn(idList instanceof List ? (List) idList : new ArrayList<>(idList));
            handlerExmaple(example);
            return getMapper().selectByExample(example);
        }
    }

    @Override
    public boolean updateById(Record record) {
        if (record == null) {
            return false;
        } else {
            Example example = getExampleNewInstance();
            example.createCriteria().andIdEqualTo(record.getId());
            handlerExmaple(example);
            return getMapper().updateByExample(record, example) == 0 ? false : true;
        }
    }

    @Override
    public boolean updateByIdSelective(Record record) {
        if (record == null) {
            return false;
        } else {
            Example example = getExampleNewInstance();
            example.createCriteria().andIdEqualTo(record.getId());
            record.setId(null);
            handlerExmaple(example);
            return getMapper().updateByExampleSelective(record, example) == 0 ? false : true;
        }
    }

    @Override
    public int deleteById(String... ids) {
        if (isNull(ids)) {
            return 0;
        } else {
            Example example = getExampleNewInstance();
            example.createCriteria().andIdIn(Arrays.asList(ids));
            handlerExmaple(example);
            return getMapper().deleteByExample(example);
        }
    }

    private void handlerExmaple(Example example) {
        List<BaseCriteria> criteriaList = example.getOredCriteria();
        for (BaseCriteria criteria:criteriaList) {
//            criteria.andCareUnitIdEqualTo();
        }
    }


}